﻿#region Copyright (c) 2003 - 2008, Andreas Mueller
/////////////////////////////////////////////////////////////////////////////////////////
// 
// Copyright (c) 2003 - 2008, Andreas Mueller.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// Contributors:
//    Andreas Mueller - initial API and implementation
//
// 
// This software is derived from software bearing the following
// restrictions:
// 
// Copyright (c) 1994
// Hewlett-Packard Company
// 
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies and
// that both that copyright notice and this permission notice appear
// in supporting documentation.  Hewlett-Packard Company makes no
// representations about the suitability of this software for any
// purpose.  It is provided "as is" without express or implied warranty.
// 
// 
// Copyright (c) 1996,1997
// Silicon Graphics Computer Systems, Inc.
// 
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies and
// that both that copyright notice and this permission notice appear
// in supporting documentation.  Silicon Graphics makes no
// representations about the suitability of this software for any
// purpose.  It is provided "as is" without express or implied warranty.
// 
// 
// (C) Copyright Nicolai M. Josuttis 1999.
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
// 
/////////////////////////////////////////////////////////////////////////////////////////
#endregion


using System.Diagnostics;
using NUnit.Framework;
using System.Collections;
using NUnit.Framework.SyntaxHelpers;


namespace NStl.NUnit.Support.SupportTests.NUnitRegression
{
    [DebuggerDisplay("ID = {ID}")]
    internal class A
    {
        internal A(){}
        internal A(int id) {ID = id; }

        internal static int idGen;
        internal int ID = idGen++;
        public override bool Equals(object obj)
        {
            A rhs = obj as A;
            if (rhs == null)
                return false;
            return ID.Equals(rhs.ID);
        }
        public override int GetHashCode()
        {
            return ID.GetHashCode();
        }
    }

    [TestFixture]
    public class CollectionContainsNullFixture
    {
        private ICollection collectionContainingNull;
        [SetUp]
        public void SetUp()
        {
            collectionContainingNull = new CollectionAdapter(new A(), null, new A());
        }
        
        [Test]
        public void AllItemsAreNotNull()
        {
            ICollection col = new CollectionAdapter(new A(), new A(), new A());
            CollectionAssert.AllItemsAreNotNull(col);

            try
            {
                CollectionAssert.AllItemsAreNotNull(collectionContainingNull);
                Assert.That(collectionContainingNull, Is.All.Not.Null);
            }
            catch(AssertionException){}
        }
        [Test]
        public void AllItemsAreUnique()
        {
            CollectionAssert.AllItemsAreUnique(collectionContainingNull);
        }
        

        [Test]
        public void AreEqual()
        {
            // same content, same order
            ArrayList copy = new ArrayList(collectionContainingNull);
            ICollection expected = new CollectionAdapter(copy);
            CollectionAssert.AreEqual(expected, collectionContainingNull);
            Assert.That(collectionContainingNull, Is.EqualTo(expected));
        }
        [Test]
        public void AreNotEqual()
        {
            // same content, same order
            ArrayList copy = new ArrayList(collectionContainingNull);
            copy[0] = null;
            ICollection expected = new CollectionAdapter(copy);
            CollectionAssert.AreNotEqual(expected, collectionContainingNull);
            Assert.That(collectionContainingNull, Is.Not.EqualTo(expected));
        }
        [Test]
        public void AreEquivalent()
        {
            //SameAsserter content, different order
            ArrayList copy = new ArrayList(collectionContainingNull);
            copy.Reverse();
            ICollection expected = new CollectionAdapter(copy);
            CollectionAssert.AreEquivalent(expected, collectionContainingNull);
            Assert.That(collectionContainingNull, Is.EquivalentTo(expected));
        }
        [Test]
        public void AreNotEquivalent()
        {
            //SameAsserter content, different order
            ArrayList copy = new ArrayList(collectionContainingNull);
            copy.Reverse();
            copy[copy.Count-1] = new A();
            ICollection expected = new CollectionAdapter(copy);
            CollectionAssert.AreNotEquivalent(expected, collectionContainingNull);
            Assert.That(collectionContainingNull, Is.Not.EquivalentTo(expected));
        }
        [Test]
        public void Contains()
        {
            foreach (A a in collectionContainingNull)
            {
                CollectionAssert.Contains(collectionContainingNull, a);
                Assert.That(collectionContainingNull, Has.Member(a));
            }
        }
        [Test]
        public void DoesNotContain()
        {
            CollectionAssert.DoesNotContain(collectionContainingNull, new A());

            try
            {
                CollectionAssert.DoesNotContain(collectionContainingNull, new A(A.idGen - 1));
                Assert.Fail();
            }
            catch(AssertionException)
            {
            }
        }
        [Test]
        public void IsEmpty()
        {
            ICollection emptyCol = new CollectionAdapter();
            CollectionAssert.IsEmpty(emptyCol);
            Assert.That(emptyCol, Is.Empty);

            try
            {
                CollectionAssert.IsEmpty(new CollectionAdapter(null, null));
            }
            catch (AssertionException)
            {}
        }
        [Test]
        public void IsNotEmpty()
        {
            ICollection notEmptyCol = new CollectionAdapter(null, null);
            CollectionAssert.IsNotEmpty(notEmptyCol);
            Assert.That(notEmptyCol, Is.Not.Empty);
            try
            {
                CollectionAssert.IsNotEmpty(new CollectionAdapter());
            }
            catch (AssertionException)
            { }
        }
        [Test]
        public void IsSubsetOf()
        {
            ArrayList copy = new ArrayList(collectionContainingNull);
            copy.RemoveAt(0);

            ICollection subset = new CollectionAdapter(copy);

            CollectionAssert.IsSubsetOf(subset, collectionContainingNull);
            Assert.That(subset, Is.SubsetOf(collectionContainingNull));
        }

        [Test]
        public void IsNotSubsetOf()
        {
            ICollection subset = new CollectionAdapter(null, new A());

            // TWISTED parameters. See NUnitRegressionFixture.IsSubsetOf !!!
            CollectionAssert.IsNotSubsetOf(collectionContainingNull, subset);
            Assert.That(subset, Is.Not.SubsetOf(collectionContainingNull));
        }
    }
}
